﻿using System;
using System.Collections.Generic;
using System.Text;
using MySql.Data.MySqlClient;

namespace ServerGps
{
    class Service
    {
        private DBService db_service;
        private EMayService emay_service;
        private string user_password = "e10adc3949ba59abbe56e057f20f883e";
        private static Service singleton;
        public static Service getInstance() {
            if (singleton == null) {
                singleton = new Service();
            }
            return singleton;
        }
        public Service()
        {
            db_service = DBService.getInstance();
            emay_service = EMayService.getInstance();
        }
        public void ProcessMsgRecved(string phone_num, string content) {
            Console.Out.WriteLine(DateTime.Now.ToString() + " 电话号码: " + phone_num + " 内容: " + content);
            content.Trim();
            content.Replace(" ", "");
            content.Replace('O', '0');
            content.Replace('o', '0');
            content.Replace('L', '1');
            content.Replace('l', '1');
            string db_name;
            //string daili = content.Substring(1, content.Length - 1);
            string daili = content;
            string site;
            char db_tag = content[0];
            if (db_tag == 'd' || db_tag == 'D' || db_tag == '1') {
                db_name = "wwwdzxuecom";
                site = "www.dzxue.com";
            }
            else if (db_tag == 'k' || db_tag == 'K' || db_tag == '6')
            {
                db_name = "wwwkxxeducom";
                site = "www.kxxedu.com";
            }
            else if (db_tag == 's' || db_tag == 'S' || db_tag == '5')
            {
                db_name = "wwwsuishixuecom";
                site = "www.suishixue.com";
            }
            else if (db_tag == 'y' || db_tag == 'Y' || db_tag == '9')
            {
                db_name = "wwwyxtjywcom";
                site = "www.yxtjyw.com";
            }
            else if (db_tag == 't' || db_tag == 'T' || db_tag == '7')
            {
                db_name = "wwwyishieducom";
                site = "www.yishiedu.com";
            }
            else {
                //log_error(0, phone_num, content);
                return;
            }
            db_service.useDB(db_name);
            MySqlDataReader reader = db_service.query("select count(*) from phpcms_member where username = '" + phone_num + "'");
            while (reader.Read()) {
                if (reader[0].ToString() != "0") {
                    int result = emay_service.send(phone_num, "用户名" + phone_num + "已经注册，请换另一个手机号进行注册");
                    reader.Close();
                    Console.Out.WriteLine(DateTime.Now.ToString() + " 电话号码: " + phone_num + "已经注册");
                    return;
                }
            }
            reader.Close();
            reader = db_service.query("select count(*) from phpcms_member_info where my_valid = 1 and my_salecode = '" + content.Trim() + "'");
            while (reader.Read()) {
                if (reader[0].ToString() == "0") {
                    reader.Close();
                    Console.Out.WriteLine(DateTime.Now.ToString() + " 电话号码: " + phone_num + "促销码不正确");
                    log_error(0, phone_num, content);
                    //db_service.execute("insert into error_member (username, message) values ('" + phone_num + "', '" + content + "'");
                    return;
                }
            }
            reader.Close();
            DateTime one_week_later = DateTime.Now.AddDays(7);
            db_service.execute("insert into phpcms_member (username, password, enddate) values ('" + phone_num + "', '" + user_password + "', '" + one_week_later.ToString() + "')");
            int id = System.Int32.Parse(db_service.executeScalar("select userid from phpcms_member where username = '" + phone_num + "'").ToString());
            db_service.execute("insert into phpcms_member_info(userid, my_daili) values('" + id + "', '" + daili + "')");

            int result1 = emay_service.send(phone_num, "用户名:" + phone_num + ", 密码:123456，登陆网站:" + site);
            ++result1;
            Console.Out.WriteLine(DateTime.Now.ToString() + " 电话号码: " + phone_num + "注册成功");
            //string sql = "insert into phpcms_member (username, password, enddate) values ('" + phone_num + "', '" + user_password + "', '" + one_week_later.ToString() + "'";
            //sql = "";
        }
        private void log_error(int code, string phone_num, string msg) {
            db_service.useDB("mobilemember");
            db_service.execute("insert into error_member (username, message) values('" + phone_num + "', '" + msg + "')");
        }
        public static void Main1() {
            Service s = new Service();
            s.ProcessMsgRecved("13811776394", "dwytest1");
            s.ProcessMsgRecved("13811776394", "swytest2");
            s.ProcessMsgRecved("13811776394", "ywytest2");
        }
    }
}
